package com.huayal.mall.product.controller;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

import com.huayal.mall.product.entity.AttrEntity;
import com.huayal.mall.product.service.AttrService;
import com.huayal.mall.product.service.CategoryService;
import com.huayal.mall.product.vo.AttrGroupRelationVo;
import com.huayal.mall.product.vo.AttrGroupWithAttrsVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import com.huayal.mall.product.entity.AttrGroupEntity;
import com.huayal.mall.product.service.AttrGroupService;
import com.huayal.common.utils.PageUtils;
import com.huayal.common.utils.R;


/**
 * 属性分组
 *
 * @author huayal
 * @email efry@qq.com
 * @date 2024-11-01 20:27:01
 */
@RestController
@RequestMapping("product/attrgroup")
public class AttrGroupController {
    @Autowired
    private AttrGroupService attrGroupService;

    @Autowired
    private AttrService attrService;

    @Autowired
    private CategoryService categoryService;

    //http://localhost:8060/app/product/attrgroup/225/withattr?t=1732714069878
    @GetMapping("/{catalogId}/withattr")
    public R getWithattr(@PathVariable("catalogId") Long catalogId){
        //根据三级分类编号获取属性组和属性组的属性信息
        List<AttrGroupWithAttrsVo> list=attrGroupService.getAttrgroupWithAttrsByCatalogId(catalogId);
        return R.ok().put("data",list);
    }

    //http://localhost:8060/app/product/attrgroup/attr/relation
    @PostMapping("/attr/relation")
    public R addAttrRelation(@RequestBody List<AttrGroupRelationVo> vos){
        attrGroupService.addAttrRelation(vos);
        return R.ok();
    }


    //http://localhost:8060/app/product/attrgroup/1/noattr/relation?t=1732278881371&page=1&limit=10&key=

    @GetMapping("/{attrGroupId}/noattr/relation")
    public R getNoAttrRelation(@RequestParam Map<String, Object> params,@PathVariable("attrGroupId") Long attrGroupId){

        PageUtils page = attrService.getNoAttrRelation(params,attrGroupId);

        return R.ok().put("page", page);
    }


    //   attr/relation/delete
    @PostMapping("/attr/relation/delete")
    public R deleteAttrGroupRelation(@RequestBody AttrGroupRelationVo[] vos){

        attrGroupService.deleteAttrGroupRelation(vos);
        return R.ok();
    }

    /**
     * 获取关联的attrEntity
     * @param attrGroupId
     * @return
     */
    @GetMapping("/{attrGroupId}/attr/relation")
    public R AttrList(@PathVariable("attrGroupId") Long attrGroupId){

        List<AttrEntity> list=attrGroupService.getAttrListByAttrGroupId(attrGroupId);
        return R.ok().put("data",list);
    }

    /**
     * 列表
     */
    @RequestMapping("/list/{catelogId}")
    public R list(@RequestParam Map<String, Object> params,@PathVariable("catelogId") Long catelogId) {
        PageUtils page = attrGroupService.queryPage(params,catelogId);


        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{attrGroupId}")
    public R info(@PathVariable("attrGroupId") Long attrGroupId) {
        AttrGroupEntity attrGroup = attrGroupService.getById(attrGroupId);
        //显示商品类别级层数据
        attrGroup.setShowCatelogId(categoryService.getCatIds(attrGroup.getCatelogId()));

        return R.ok().put("attrGroup", attrGroup);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    public R save(@RequestBody AttrGroupEntity attrGroup) {
        attrGroupService.save(attrGroup);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    public R update(@RequestBody AttrGroupEntity attrGroup) {
        attrGroupService.updateById(attrGroup);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    public R delete(@RequestBody Long[] attrGroupIds) {
        attrGroupService.removeByIds(Arrays.asList(attrGroupIds));

        return R.ok();
    }

}
